package com.fingard.dsp.bank.directConfig;


import com.fingard.dsp.DSPSet;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;

import java.util.ArrayList;
import java.util.HashMap;

public class FileSaveSet {
	
	/**
		1、saveType如果是remoteHost-远程目录，配置mainHostServer；
		2、saveType如果是localhost-本地目录，配置mainDir；
		3、saveType如果是shareDir-共享目录，配置shareDir；
		4、saveType如果是custom.fileServer-客制化文件服务器，配置uploadUrl和downloadUrl，还有上一级的customCode
		  localhost：本地，即mainDir配置的目录；
		  remoteHost：远程回单主DSP，即mainHostServer配置的DSP服务地址；
		  shareDir：共享目录，即shareDir配置的共享目录
	 **/
	public String id;
	public String saveType;
	private String defMainDir;
	private HashMap<String,String> mapMainDir;
	
	/**取服务地址，一般用于日志*/
	public String getServerAddrForLog(){
		if("ftp".equals(saveType)||"sftp".equals(saveType)){
			if(ftpSet!=null){
				return ftpSet.serverIP+":"+ftpSet.port;
			}else{
				return "未设置ftpSet";
			}
		}else if("shareDir".equalsIgnoreCase(saveType)){
			return this.shareDir;
		}else if("oss".equalsIgnoreCase(saveType)){
			if(ossSet != null){
				return ossSet.endpoint;
			}else{
				return "未设置ossSet";
			}
		}else if("custom.fileServer".equalsIgnoreCase(saveType)){
			return StringHelper.joinNotEmpty(this.uploadUrl, this.downloadUrl, customServerUrl);
		}else if("remoteHost".equalsIgnoreCase(saveType)){
			return this.mainHostServer;
		}else if("copyToDir".equalsIgnoreCase(saveType)){
			return this.defMainDir;
		}else{
			return "未能识别服务地址";
		}
	}
	
	/**根据银行代码取回单保存主目录
	 * @param pBank，银行代码，如102工行，308招行*/
	public String getMainDir(String pBank){
		if(mapMainDir.containsKey(pBank)){
			return mapMainDir.get(pBank);
		}
		return defMainDir;
	}
	/**弃用*/
	public String fileNameFormat;
	/**pro版本采用该配置*/
	public String filePathFormat;
	public String shareDir;
	public String pushServerUrl;
	
	/**回单主DSP服务地址，各DSP将回单文件发送至该DSP*/
	public String mainHostServer;
	/**客户代码，用于保存至客制化文件服务器时，指定客户*/
	public String customCode;
	public String uploadUrl;
	public String downloadUrl;
	public String username;
	public String systemName;
	public String readTimeout;
	private String charset;

	public String getCharset(String pDef){
		if(StringHelper.hasAnyChar(charset)){
			return charset;
		}else{
			return pDef;
		}
	}
	
	public String customServerUrl;

	public String customServerIP;

	public String customServerPort;
	
	public boolean isTransmitToMainHost;
	
	public OssSet ossSet;
	
	public FtpSet ftpSet;
	
	private HashMap<String,String> paramList;
	
	/**设置秒，返回秒*/
    public int getReadTimeoutSec(int p_defSec, StringBuilder p_sb) {
        //秒
        int timeout = p_defSec;
        if(StringHelper.hasAnyChar(readTimeout)){
        	try {
                timeout = Integer.parseInt(readTimeout);
            } catch (Exception ex) {
            	p_sb.append(readTimeout+"转int异常："+ex.getMessage()+"\r\n");
            }
        }
        return timeout;
    }
	
	public void fromXElement(XElement p_xe) throws Exception{
		id = p_xe.getSingleTextNotNull("id");
		saveType = p_xe.getSingleTextNotNull("saveType");
		charset = p_xe.getSingleTextNotNull("charset");
		//mainDir = DSPSet.replaceFilePath(p_xe.getSingleTextNotNull("mainDir"));
		mapMainDir = new HashMap<String,String>();
		ArrayList<XElement> listXe = p_xe.getChildList("mainDir");
		if(listXe!=null && listXe.size()>0){
			for(XElement eachXe : listXe){
				String eachFor = eachXe.getAttrValue("for");
				if(StringHelper.isNullOrEmpty(eachFor)){
					defMainDir = DSPSet.replaceFilePath(eachXe.innerText);
				}else{
					mapMainDir.put(eachFor, DSPSet.replaceFilePath(eachXe.innerText));
				}
			}
		}
		shareDir = p_xe.getSingleTextNotNull("shareDir");
		if(!shareDir.trim().toLowerCase().startsWith("smb")){
			shareDir = DSPSet.replaceFilePath(shareDir);
		}
		pushServerUrl = p_xe.getSingleTextNotNull("pushServerUrl");
		mainHostServer = p_xe.getSingleTextNotNull("mainHostServer");
		customCode = p_xe.getSingleTextNotNull("customCode");
		uploadUrl = p_xe.getSingleTextNotNull("uploadUrl");
		downloadUrl = p_xe.getSingleTextNotNull("downloadUrl");
		customServerUrl = p_xe.getSingleTextNotNull("customServerUrl");
        customServerIP = p_xe.getSingleTextNotNull("customServerIP");
        customServerPort = p_xe.getSingleTextNotNull("customServerPort");
		readTimeout = p_xe.getSingleTextNotNull("readTimeout");
		username = p_xe.getSingleTextNotNull("username");
		systemName = p_xe.getSingleTextNotNull("systemName");
		fileNameFormat = p_xe.getSingleTextNotNull("fileNameFormat");
		filePathFormat = p_xe.getSingleTextNotNull("filePathFormat");
		String isTransTo = p_xe.getSingleTextNotNull("isTransmitToMainHost").toLowerCase();
		if("true".equals(isTransTo) || "1".equals(isTransTo)){
			isTransmitToMainHost = true;
		}else{
			isTransmitToMainHost = false;
		}
		ossSet = new OssSet();
		XElement xeOss = p_xe.getSingleChild("ossSet");
		if(xeOss!=null){
			ossSet.fromXElement(xeOss);
		}
		
		XElement xeFtp = p_xe.getSingleChild("ftpSet");
		if(xeFtp !=null){
			ftpSet = new FtpSet();
			ftpSet.fromXElement(xeFtp);
		}
		paramList = new HashMap<String,String>();
		XElement params = p_xe.getSingleChild("paramList");
		if(params != null){
			ArrayList<XElement> pList = params.getAllChildList();
			if(pList!=null && pList.size()>0){
				for(XElement pmXe : pList){
					String pmKey = pmXe.name + "-" + pmXe.getAttrValue("for");
					paramList.put(pmKey.toLowerCase(), pmXe.innerText);
				}
			}
		}
	}
	
	public String getParam(String pKey, String pFor){
		if(paramList!=null && paramList.size()>0){
			String firstKey = pKey+"-"+pFor;
			String[] chkKeys = new String[]{firstKey.toLowerCase(),"","*"};
			for(String ehKey : chkKeys){
				if(paramList.containsKey(ehKey)){
					return paramList.get(ehKey);
				}
			}
		}
		return "";
	}
}
